package com.jghz.transfer.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.importexcel.model.CellWrapper;
import com.bstek.bdf2.importexcel.model.ExcelDataWrapper;
import com.bstek.bdf2.importexcel.model.RowWrapper;
import com.bstek.bdf2.importexcel.processor.IExcelProcessor;
import com.jghz.framework.utils.AppUtils;
import com.jghz.framework.utils.ObjectUtils;
import com.jghz.transfer.dao.CashFlowDao;
import com.jghz.transfer.entity.Tbplateformcash;

@Service(CashFlowExcelImport.BEAN_ID)
public class CashFlowExcelImport implements IExcelProcessor {

	@Resource
	private CashFlowDao cashFlowDao;

	public static final String BEAN_ID = "sh.ImportExcelTransPlateFormCash";

	@Override
	public String getName() {
		return "资金流导入模板";
	}

	@Override
	@Transactional
	public int execute(ExcelDataWrapper excelDataWrapper) throws Exception {
		Collection<RowWrapper> rowWrappers = excelDataWrapper.getRowWrappers();// 获得row数据集合
		List<Tbplateformcash> listForSave = new ArrayList<>();
		for (RowWrapper rowWrapper : rowWrappers) {

			Tbplateformcash tbplateformcash = new Tbplateformcash();
			tbplateformcash.setId(AppUtils.generateUniqueKey());
			tbplateformcash.setCompId(AppUtils.getLoginUserCompId());
			tbplateformcash.setStatus("N");
			Collection<CellWrapper> cells = rowWrapper.getCellWrappers();// 获得每列CELL合集
			for (CellWrapper cellWrapper : cells) {
				doProcessCell(cellWrapper, tbplateformcash);
			}
			listForSave.add(tbplateformcash);
		}

		for (Tbplateformcash tbplateformcash : listForSave) {
			cashFlowDao.save(tbplateformcash);
		}

		return listForSave.size();
	}

	private void doProcessCell(CellWrapper cellWrapper, Tbplateformcash tbplateformcash) {
		Object value = cellWrapper.getValue();
		switch (cellWrapper.getColumn()) {
		case 1:
			if (value instanceof String) {
				tbplateformcash.setSenderCode(ObjectUtils.toString(value));
			}
			break;
		case 2:
			if (value instanceof String) {
				tbplateformcash.setDocumentNumber(ObjectUtils.toString(value));
			}
			break;
		case 3:
			if (value instanceof String) {
				tbplateformcash.setCarrier(ObjectUtils.toString(value));
			}
			break;
		case 4:
			if (value instanceof String) {
				tbplateformcash.setVehicleNumber(ObjectUtils.toString(value));
			}
			break;
		case 5:
			if (value instanceof String) {
				tbplateformcash.setLicensePlateTypeCode(ObjectUtils.toString(value));
			}
			break;
		case 6:
			if (value instanceof String) {
				tbplateformcash.setShippingNoteNumber(ObjectUtils.toString(value));
			}
			break;
		case 7:
			if (value instanceof String) {
				tbplateformcash.setRemark(ObjectUtils.toString(value));
			}
			break;
		case 8:
			if (value instanceof String) {
				tbplateformcash.setPaymentMeansCode(ObjectUtils.toString(value));
			}
			break;
		case 9:
			if (value instanceof String) {
				tbplateformcash.setBankCode(ObjectUtils.toString(value));
			}
			break;
		case 10:
			if (value instanceof String) {
				tbplateformcash.setSequenceCode(ObjectUtils.toString(value));
			}
			break;
		case 11:
			tbplateformcash.setMonetaryAmount(AppUtils.getBigDecimal(value));
			break;
		case 12:
			if (value instanceof String) {
				tbplateformcash.setDateTime(ObjectUtils.toString(value));
			}
			break;
		default:
			break;
		}
	}

}
